﻿using UnityEngine;
using System.Collections;

namespace EnemyClasses
{
	/// <summary>
	/// Enemy factory. This spawns the enemies.
	/// </summary>
	public class EnemyFactory : MonoBehaviour
	{
		public int TotalAmount = 100;
		public int MaxSpawnedAtOnce = 20;
		public int SpawnRadiusSize = 50;

		public GameObject NormalEnemyPrefab;
		public GameObject BossPrefab;
		
		public Transform spawnArea;
		private EnemyFactory m_instance;

		void Start ()
		{
			m_instance = this;
		}

		void Update ()
		{
			if (Enemy.NumberOfEnemies < MaxSpawnedAtOnce) {
				SpawnEnemy ();
			}
		}

		/// <summary>
		/// Spawns the enemy.
		/// </summary>
		void SpawnEnemy ()
		{
			Vector3 spawnLocation = Random.insideUnitCircle * SpawnRadiusSize;
			spawnLocation.z = spawnLocation.y;
			spawnLocation = spawnArea.position + spawnLocation;
			spawnLocation.y = 50.0f;
			Debug.Log("SL: " + spawnLocation);
			RaycastHit hitinfo;
			if (Physics.Raycast (spawnLocation, Vector3.down, out hitinfo, 100.0f)) {
				spawnLocation = hitinfo.point;
				Debug.Log ("Spawning Enemy");
				Instantiate (NormalEnemyPrefab, spawnLocation, Quaternion.identity);
			}

		}

		/// <summary>
		/// Gets or sets the spawn area.
		/// </summary>
		/// <value>The spawn area.</value>
		public Transform SpawnArea {
			get {
				return spawnArea;
			}
			set {
				spawnArea = value;
			}
		}

		/// <summary>
		/// Gets the instance.
		/// </summary>
		/// <value>The instance.</value>
		public EnemyFactory Instance {
			get {
				return m_instance;
			}
		}
	}
}
